﻿using System;
using UnityEngine;
namespace Core
{
    public class Timer : IUpdate
    {
        private float endTime;
        private Action action;

        public bool isRuning { get; private set; } = true;
        public Timer(float endTIme)
        {
            this.endTime = endTIme;
        }

        public Timer(float endTime, Action action)
        {
            this.endTime = endTime;
            this.action = action;
        }

        public void Stop()
        {
            isRuning = false;
        }

        void IUpdate.Update()
        {
            if (!isRuning)
                return;
            endTime -= Time.deltaTime;
            if (endTime <= 0)
            {
                action?.Invoke();
                isRuning = false;
            }
        }
    }
}